package com.three.vip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.*;
import com.three.core.vo.R;
import com.three.vip.feign.AdminUserFeign;
import com.three.vip.mapper.CarMapper;
import com.three.vip.mapper.CarStoreMapper;
import com.three.vip.mapper.FrontUserStoreMapper;
import com.three.vip.service.CarService;
import com.three.vip.service.FrontUserService;
import com.three.vip.vo.AddCarVo;
import com.three.vip.vo.CarBindVo;
import com.three.vip.vo.SearchFormVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ：JinShiYan
 * @date ：Created in 2025/3/12 15:48
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private FrontUserService frontUserService;

    @Autowired
    private CarStoreMapper carStoreMapper;

    @Autowired
    private FrontUserStoreMapper frontUserStoreMapper;

    @Autowired
    private AdminUserFeign adminUserFeign;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest request;

    @Override
    public R<IPage<Car>> selectAll(SearchFormVo searchFormVo) {
        //获取Token
        String token = request.getHeader("token");
        // 获取用户名
        Object user = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> byName = adminUserFeign.getByName((String) user);
        Integer userId = byName.getData().getUserId();
        Page<Car> page = new Page<>(searchFormVo.getCurrentPage(), searchFormVo.getPageSize());
        IPage<Car> p = carMapper.selectCarByCondition(page, searchFormVo.getCarNo(), userId);

        return new R<>(200, "查询成功", p);
    }

    @Override
    public R<List<CarBindVo>> carBind() {
        R<FrontUser> userInfo = frontUserService.getUserInfo();
        int userId = userInfo.getData().getUserId();
        List<CarBindVo> carBindVos = carMapper.carBind(userId);
        if (carBindVos != null){
            return new R<>(200, "查询成功", carBindVos);
        }
        return new R<>(400, "查询失败", null);
    }

    @Override
    public R<Car> findCarId(Integer carId) {
        Car car = carMapper.selectById(carId);
        if (car != null){
            return new R<>(200, "查询成功", car);
        }
        return new R<>(400, "查询失败", null);
    }

    @Override
    @Transactional
    public R<Car> addCar(AddCarVo addCarVo) {
        String carNo = addCarVo.getCarNo();
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_no", carNo);
        Car car = carMapper.selectOne(queryWrapper);
        if (car != null){
            return new R<>(200, "车牌号已存在", null);
        }
        QueryWrapper<CarStore> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("car_no", carNo);
        queryWrapper1.eq("store_id", addCarVo.getStoreId());
        CarStore carStore = carStoreMapper.selectOne(queryWrapper1);
        if (carStore != null){
            return new R<>(200, "该车已绑定该店铺", null);
        }
        QueryWrapper<FrontUserStore> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id", addCarVo.getUserId());
        queryWrapper2.eq("store_id", addCarVo.getStoreId());
        FrontUserStore frontUserStore = frontUserStoreMapper.selectOne(queryWrapper2);
        if (frontUserStore == null){
            FrontUserStore fus = new FrontUserStore();
            fus.setStoreId(addCarVo.getStoreId());
            fus.setUserId(addCarVo.getUserId());
            frontUserStoreMapper.insert(fus);
        }
        Car car1 = new Car();
        car1.setCarNo(addCarVo.getCarNo());
        car1.setUserId(addCarVo.getUserId());
        car1.setCreateTime(LocalDateTime.now());
        int insert = carMapper.insert(car1);
        Car car2 = carMapper.selectOne(queryWrapper);
        int carId = car2.getCarId();
        CarStore carStore1 = new CarStore();
        carStore1.setCarId(carId);
        carStore1.setStoreId(addCarVo.getStoreId());
        carStore1.setCarNo(carNo);
        int insert1 = carStoreMapper.insert(carStore1);


        if (insert > 0 && insert1 > 0){
            return new R<>(200, "添加成功", car2);
        }
        return new R<>(400, "添加失败", null);
    }

    @Override
    public R deleteCarNo(String carNo) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_no", carNo);
        int delete = carMapper.delete(queryWrapper);
        if (delete > 0){
            return new R<>(200, "删除成功", null);
        }
        return new R<>(400, "删除失败", null);
    }
}
